package com.qili.util;


import com.qili.base.entity.CurrentRole;
import com.qili.base.entity.CurrentUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;

import java.text.SimpleDateFormat;
import java.util.*;

import static java.util.Objects.isNull;

/**
 * @author zhuxiaomeng
 * @date 2017/12/4.
 * @email 154040976@qq.com
 * <p>
 * 管理工具类
 */
public class CommonUtil {

    /**
     * 获取当前用户
     */
    public static CurrentUser getUser() {
        org.apache.shiro.subject.Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        return (CurrentUser) session.getAttribute("currentPrincipal");
    }

    /**
     * @param
     * @Author:Zhangshuo
     * @Description:获取当前用户ID
     * @Date:15:30 2020/11/25
     */
    public static String getUserId() {
        org.apache.shiro.subject.Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        CurrentUser currentUser = (CurrentUser) session.getAttribute("currentPrincipal");
        if (isNull(currentUser)) {
            return "";
        } else {
            return currentUser.getId();
        }
    }

    /**
     * @param
     * @Author:liuyang
     * @Description:获取当前用户所有的角色
     * @Date:15:30 2021/03/18
     */
    public static List<String> getUserRoleList() {
        List<String> userRoleList = new ArrayList<>();
        CurrentUser user = getUser();
        List<CurrentRole> currentRoleList = user.getCurrentRoleList();
        for (CurrentRole cRole : currentRoleList) {
            userRoleList.add(cRole.getRoleName());
        }
        return userRoleList;
    }

    public static String getRandomUUID(){
        return UUID.randomUUID().toString().replace("-","").toUpperCase(Locale.ROOT);
    }

    public static String getCurrentTimeStr() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }
}